<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="院系">
              <a-select allowClear  v-model="queryParam.ssyxm" @change="yxChange" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="专业">
              <a-select allowClear  v-model="queryParam.sszym" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item
              label="学号"
              
            >
              <a-input v-model="queryParam.xh"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item
              label="所在年级"
              
            >
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.sznj">
                <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item
                label="学位类别"
               
              >
                <a-select :allowClear="true" disabled  showSearch optionFilterProp="children" v-model="queryParam.pymsm">
                  <a-select-option v-for="i in TYPE0347" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="学生来源"
              >
                <a-select :allowClear="true" v-model="queryParam.xslxm">
                  <a-select-option v-for="i in TYPE0503" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="姓名"
                
              >
                <a-input v-model="queryParam.xm"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="性别"
               
              >
                <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.xbm">
                  <a-select-option v-for="i in TYPE0011" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="导师职工号"
                
              >
                <a-input v-model="queryParam.dszgh"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="导师姓名"
                
              >
                <a-input v-model="queryParam.dsxm"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="实践基地"
               
              >
                <a-input v-model="queryParam.sjjd"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="实践单位"
                
              >
                <a-input v-model="queryParam.sjdw"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="专业实践时长"
                
              >
                <a-col :span="10">
                  <a-input-number style="width:100%" v-model="queryParam.startIndex" :min="0"/>
                </a-col> 
                
                <a-col :span="4" style="text-aline:center">
                  <p style="text-align:center">-</p>
                </a-col>
                <a-col :span="10">
                  <a-input-number style="width:100%" v-model="queryParam.endIndex" :min="0"/>
                </a-col> 
                
              </a-form-item>
            </a-col>
            <!-- <a-col :md="6" :sm="12">
              <a-form-item
                label="审核状态"
                :labelCol="{span: 8}"
                :wrapperCol="{span: 15, offset: 1}"
              >
                <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.xbm">
                  <a-select-option v-for="i in TYPE0516" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col> -->
          </template>
          <a-col :md="24" :sm="24">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
              <a-button v-if="!isYxRole" type="primary" @click="showBatchBack" >退回</a-button>
              <a-button @click="queryParam = {};$refs.table.refresh()" style="margin:0 10px">重置</a-button>

              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        :pageSize="parameter.pageSize"
        :pageNum="parameter.pageNum"
        :scroll="{x: 2100}"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <span slot="zysjsqsj" slot-scope="text">
          {{ text?text.slice(0, 10):" " }}
        </span>
        <span slot="nsjlbmc" slot-scope="text,record">
          {{ text === '其他' ? record.nsjlbqt : text }}
        </span>
        <span slot="zysjqzsj" slot-scope="text, record" :style="{color:(new Date(record.zysjjssj.slice(0, 10)) - new Date(record.zysjkssj.slice(0, 10))) / (1000 * 60 * 60 * 24) < 364 ? 'red' : ''}">
          {{ record.zysjkssj.slice(0, 10) }} - {{ record.zysjjssj.slice(0, 10) }} ({{record.ts}}天)
        </span>
        <template slot="id" slot-scope="id">
          <a @click="toDetail(id, 0)">查看</a>
        </template>
      </s-table>
    </a-card>

    <batch-check :visible="checkVisible" @cancel="checkVisible = false" @confirm="confirmCheck" />

  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import batchCheck from './batchCheck'
import searchMixin from '@/utils/searchRemain'

export default {
  mixins: [ searchMixin ],
  data () {
    return {
      advanced: true,
      queryParam: {
        pymsm: '02'
      },
      columns: [
        {
          title: '学号',
          dataIndex: 'xh',
          width: 160
        },
        {
          title: '姓名',
          dataIndex: 'xm',
          width: 100
        },
        {
          title: '培养单位',
          dataIndex: 'ssyxmc',
          width: 200
        },
        {
          title: '申请时间',
          dataIndex: 'zysjsqsj',
          scopedSlots: { customRender: 'zysjsqsj' },
          width: 200
        },
        {
          title: '拟实践类别',
          dataIndex: 'nsjlbmc',
          scopedSlots: { customRender: 'nsjlbmc' },
          width: 220
        },
        {
          title: '实践基地名称',
          dataIndex: 'sjjdmc',
          width: 250
        },
        {
          title: '实践单位全称',
          dataIndex: 'sjdwqc',
          width: 250
        },
        {
          title: '专业实践起止时间',
          scopedSlots: { customRender: 'zysjqzsj' },
          width: 220
        },
        {
          title: '实践单位指导教师姓名',
          dataIndex: 'sjzdjsxm',
          width: 180
        },
        {
          title: '专业技术职称',
          dataIndex: 'zyjszc',
          width: 150
        },
        {
          title: '审核状态',
          dataIndex: 'shztmc',
          width: 200
        },
        // {
        //   title: '审核说明',
        //   dataIndex: 'lwtm',
        //   width: 150
        // },
        {
          title: '操作',
          dataIndex: 'id',
          scopedSlots: { customRender: 'id' },
          fixed:'right',
          width: 80
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      selectedRowKeys: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(parameter, this.queryParam)
        if (params.timeArange) {
          params.zysjkssj = params.timeArange[0].format('YYYY-MM-DD')
          params.zysjjssj = params.timeArange[1].format('YYYY-MM-DD')
          delete params.timeArange
        }
        params.shzt = '07'
        return this.$api.degree.zysjsqList(params).then(res => {
          res.data.rows.map(item=>{
            item.shztmc = item.shzt == '07' ? '专业学位办已备案' : (item.shzt == '08' ? '专业学位办未备案':item.shztmc)
          })
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectMenu: {
        yxList: []
      },
      checkVisible: false,
      initDone: false
    }
  },
  components: {STable, batchCheck},
  computed: {
    isYxRole() {
      return this.$route.name.includes('院系审核')
    },
    ...mapState({
      TYPE0347: state => state.app['TYPE0347'],
      TYPE0126: state => state.app['TYPE0126'],
      TYPE0011: state => state.app['TYPE0011'],
      TYPE0516: state => state.app['TYPE0516'],
      TYPE0503: state => state.app['TYPE0503'],
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0347')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0011')
    this.$store.dispatch('app/setDictionary', 'TYPE0516')
    this.$store.dispatch('app/setDictionary', 'TYPE0503')
  //   console.log(this.$route)
  //   this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yxList: res[0].data
        }
        if(this.selectMenu.yxList && this.selectMenu.yxList.length == 1){
          this.$set(this.queryParam, 'ssyxm', this.selectMenu.yxList[0].dwh)
        }
        if (this.queryParam.ssyxm) {
          this.$api.base.majorAll({lsdwh: this.queryParam.ssyxm}).then(res => {
            this.$set(this.selectMenu, 'zy', res.data)
          })
        }
        this.initDone = true
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.$set(this.queryParam, 'sszym', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zy', [])
      }
    },
    toDetail(id, isCheck) {
      this.$router.push(`/degree/profession/practiceApply/detail?id=${id}&isCheck=${isCheck}`)
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
    },
    showBatchBack() {
      if (!this.selectedRowKeys.length) {
        return this.$message.warning('请选择需要退回的专业申请')
      }
      this.$confirm({
        title: '退回提示',
        content: '若学生已提交实践结果评价，会被重置为未提交状态，是否确认操作?',
        onOk:()=>{
          this.$api.degree.zysjsqBack({id: this.selectedRowKeys.join(',')}).then(res => {
            this.$message.success(res.msg)
            this.doRefresh()
          })
        },
        onCancel() {},
      });
    },
    confirmCheck(params) {
      this.$api.degree.zysjsqXwbCheck({id: this.selectedRowKeys.join(','), ...params}).then(res => {
        this.checkVisible = false
        this.$message.success(res.msg)
        this.doRefresh()
      })
    },
    toDetail(id, isCheck) {
      this.$router.push(`/degree/profession/practiceApply/detail?id=${id}&isCheck=${isCheck}&isYxRole=${this.isYxRole ? '1' : '0'}`)
    },
    doRefresh(){
      this.selectedRowKeys = []
      this.selectedRows = []
      this.$refs.table.refresh(false)
    }
  }
}
</script>

<style>

</style>
